package com.yjhb.android.sbz.dialog;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemScrollListener;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2020/11/26 16:24
 */
public class LoopDialog extends Dialog implements View.OnClickListener, OnItemScrollListener, OnItemSelectedListener {
    private final String TAG = "LoopDialog";

    private LoopView loopView;
    private TextView cancel_tv;
    private TextView submit_tv;
    private TextView title_tv;

    private List<LoopItem> items;
    private LoopItem currSelected;
    private OnSelectedListener onSelectedListener;

    public LoopDialog(@NonNull Context context) {
        super(context, R.style.mDialog);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_loop, null, false);
        loopView = view.findViewById(R.id.loopView);
        cancel_tv = view.findViewById(R.id.cancel_tv);
        submit_tv = view.findViewById(R.id.submit_tv);
        title_tv = view.findViewById(R.id.title_tv);
        setContentView(view,new ViewGroup.LayoutParams(-1, ViewGroup.LayoutParams.WRAP_CONTENT));
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        submit_tv.setOnClickListener(this);
        cancel_tv.setOnClickListener(this);
        loopView.setOnItemScrollListener(this);
        loopView.setListener(this);
    }

    public TextView getTitleView() {
        return title_tv;
    }

    @Override
    public void show() {
        super.show();
    }

    public void show(List<LoopItem> items) {
        setItems(items);
        show();
    }

    public void setItems(List<LoopItem> items) {
        if (items != null && items.size() > 0) {
            this.items = items;
            List<String> itemNames = new ArrayList<>();
            Iterator<LoopItem> iterator = items.iterator();
            while (iterator.hasNext()) {
                itemNames.add(iterator.next().itemName);
            }
            loopView.setItems(itemNames);
            loopView.setInitPosition(0);
            currSelected = this.items.get(0);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == submit_tv) {
            if (onSelectedListener != null) {
                onSelectedListener.onSelected(currSelected);
            }
            dismiss();
        } else if (v == cancel_tv) {
            dismiss();
        }
    }

    @Override
    public void onItemScrollStateChanged(LoopView loopView, int currentPassItem, int oldScrollState, int scrollState, int totalScrollY) {

    }

    @Override
    public void onItemScrolling(LoopView loopView, int currentPassItem, int scrollState, int totalScrollY) {

        ZxLogTool.i("滚动信息",currSelected.itemName);
    }

    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    @Override
    public void onItemSelected(int index) {
        ZxLogTool.i("选中",currSelected.itemName);
        currSelected = items.get(index);
    }

    public interface OnSelectedListener {
        void onSelected(LoopItem item);
    }

    public static class LoopItem {

        public Object data;
        public String itemName;


        public LoopItem(Object data, String itemName) {
            this.data = data;
            this.itemName = itemName;
        }

    }
}
